package com.lishui.waterojbackendquestionservice.controller.inner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lishui.waterojbackendmodel.model.pojo.entity.Question;
import com.lishui.waterojbackendmodel.model.pojo.entity.QuestionSubmit;
import com.lishui.waterojbackendquestionservice.service.QuestionService;
import com.lishui.waterojbackendquestionservice.service.QuestionSubmitService;
import com.lishui.waterojbackendserviceclient.service.QuestionFeignClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 该服务仅内部调用，不是给前端的
 */
@RestController
@RequestMapping("/inner")
public class QuestionInnerController implements QuestionFeignClient {

    @Resource
    private QuestionService questionService;

    @Resource
    private QuestionSubmitService questionSubmitService;

    @GetMapping("/get/id")
    @Override
    public Question getQuestionById(@RequestParam("questionId") long questionId) {
        return questionService.getById(questionId);
    }

    @GetMapping("/question_submit/get/id")
    @Override
    public QuestionSubmit getQuestionSubmitById(@RequestParam("questionId") long questionSubmitId) {
        return questionSubmitService.getById(questionSubmitId);
    }

    @PostMapping("/question_submit/update")
    @Override
    public boolean updateQuestionSubmitById(@RequestBody QuestionSubmit questionSubmit) {
        return questionSubmitService.updateById(questionSubmit);
    }

    @Override
    @PostMapping("/update")
    public boolean updateQuestionById(@RequestBody Question question) {
        return questionService.updateById(question);
    }

    @PostMapping("/count")
    public Long count(@RequestBody QuestionSubmit questionSubmit) {
        Long questionId = questionSubmit.getQuestionId();
        String judgeInfo = questionSubmit.getJudgeInfo();
        QueryWrapper<QuestionSubmit> queryWrapper = new QueryWrapper<>();
        if (questionId != null) queryWrapper.eq("question_id", questionId);
        if (judgeInfo != null) queryWrapper.like("judge_info", "Accepted");
        return questionSubmitService.count(queryWrapper);
    }
}
